#include "lb_addbook.h"
#include "ui_lb_addbook.h"


#include <QString>
#include <QDebug>
#include <QMessageBox>
#include <QPushButton>
#include <QList>
#include <QAbstractButton>
#include <QFile>
#include <QTextStream>
#include <QIODevice>
#include <QPalette>


//pass
LB_addbook::LB_addbook(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LB_addbook)
{
    ui->setupUi(this);

    //QPixmap pixmap = QPixmap("D://Qt//workspace//bookManager//pictures//xiyang.jpg").scaled(this->size());
    QPixmap pixmap = QPixmap("D://CsSoftware//Qt//__MyFiles//workspace//bookManager//pictures//xiyang.jpg").scaled(this->size());
    QPalette palette(this->palette());
    palette.setBrush(QPalette::Background,pixmap);
    this->setPalette(palette);

}

//pass
LB_addbook::~LB_addbook()
{
    delete ui;
}

//时间信息输入检查---可以检查闰年
bool LB_addbook::checkTimeWrongOrNot(QString date)
{
    QStringList Time = date.split('-');
    int check = 0;
    int wrongDay = 0;

    if(Time.at(0).size()!= 4){
        check = 1;//年份应填四位
        wrongDay = 101;
    }

    if(Time.at(1).toInt()<=0||Time.at(1).toInt()>=13){
        check = 2;//月份在1-12内
        wrongDay = 102;
    }

    switch (Time.at(1).toInt()) {
    case 1:
        if(Time.at(2).toInt()>=32||Time.at(2).toInt()<=0) wrongDay = 1;
        break;
    case 2:
        if((Time.at(0).toInt()%4==0&&Time.at(0).toInt()%100!=0)||(Time.at(0).toInt()%400==0)){
            if(Time.at(2).toInt()>=30||Time.at(2).toInt()<=0){
                wrongDay = 21;//闰年如果日期超过30天犯第一类错，记做21
            }
        }else{//如果是平年
            if(Time.at(2).toInt()>=29||Time.at(2).toInt()<=0)
                wrongDay = 22;//平年超过28天犯第二类错误，记为22
        }
        break;
    case 3:
        if(Time.at(2).toInt()>=32||Time.at(2).toInt()<=0) wrongDay = 3;
        break;
    case 4:
        if(Time.at(2).toInt()>=31||Time.at(2).toInt()<=0) wrongDay = 4;
        break;
    case 5:
        if(Time.at(2).toInt()>=32||Time.at(2).toInt()<=0) wrongDay = 5;
        break;
    case 6:
        if(Time.at(2).toInt()>=31||Time.at(2).toInt()<=0) wrongDay = 6;
        break;
    case 7:
        if(Time.at(2).toInt()>=32||Time.at(2).toInt()<=0) wrongDay = 7;
        break;
    case 8:
        if(Time.at(2).toInt()>=32||Time.at(2).toInt()<=0) wrongDay = 8;
        break;
    case 9:
        if(Time.at(2).toInt()>=31||Time.at(2).toInt()<=0) wrongDay = 9;
        break;
    case 10:
        if(Time.at(2).toInt()>=32||Time.at(2).toInt()<=0) wrongDay = 10;
        break;
    case 11:
        if(Time.at(2).toInt()>=31||Time.at(2).toInt()<=0) wrongDay = 11;
        break;
    case 12:
        if(Time.at(2).toInt()>=32||Time.at(2).toInt()<=0) wrongDay = 12;
        break;
    default:
        break;
    }

//    if(isbn.length()>14 || booktitle.length()<0) wrongDay = 50;

    return wrongDay;
}

//pass
void LB_addbook::on_pushButton_clicked()
{
    QString isbn = this->ui->le_isbn->text();
    QString booktitle = this->ui->le_bookTitle->text();
    QString author = this->ui->le_author->text();
    QString pub = this->ui->le_publisher->text();
    QString time = this->ui->le_dateAdded->text();
    QString qty = this->ui->le_qtyOnHand->text();
    QString wholesale = this->ui->le_wholesale->text();
    QString retail = this->ui->le_retail->text();


    QString content = "ISBN:" + isbn + '\n' + "书名：" +  booktitle + '\n' + "作者：" + author + '\n' + "出版社：" + pub + '\n'
            + "进书时间：" + time + '\n' + "库存量：" + qty + '\n' + "批发价：" +  wholesale + '\n' + "零售价：" + retail;
    QString cnt = isbn + " " + booktitle + " " + author + " " + pub + " " + time + " " + qty + " " + wholesale + " " + retail + '\n';

    if(checkTimeWrongOrNot(time) || isbn.length()>14 || booktitle.length()<0 || author.length()<0 || pub.length()<0 ||time.length()<0 || qty.length()<0 || wholesale.length()<0 || retail.length()<0)
    {
        QMessageBox::critical(this,"错误","信息填写不完整或错误，请重新检查","确定");
    }else{
        int ret = QMessageBox::question(this,"请确认信息",content,"确认","取消");

        if(ret == 0){
            clearUserInterFace();
            int i = 0;
            while(dataArray[i].isbn != "\0"){
                i++;
            }
            dataArray[i].isbn = isbn;
            dataArray[i].bookTitle = booktitle;
            dataArray[i].author = author;
            dataArray[i].publisher = pub;
            dataArray[i].dataAdded = time;
            dataArray[i].qtyOnHand = qty;
            dataArray[i].wholesale = wholesale;
            dataArray[i].retail = retail;
            dataArray[i].existOrNot = true;//设置该书存在
        }
    }
}

void LB_addbook::clearUserInterFace()
{
    this->ui->le_isbn->clear();
    this->ui->le_bookTitle->clear();
    this->ui->le_author->clear();
    this->ui->le_publisher->clear();
    this->ui->le_dateAdded->clear();
    this->ui->le_qtyOnHand->clear();
    this->ui->le_wholesale->clear();
    this->ui->le_retail->clear();
//    this->ui->le_isbn->setFocus();

}


void LB_addbook::on_pushButton_2_clicked()
{
    this->close();
}
